﻿/*
 * DataCenter.cs
 * 
 * Copyright (C) 2011 Elena Shishkina
 * 
 * This file is part of ServerInventory.
 * 
 * ServerInventory is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ServerInventory is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ServerInventory.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace ServerInventory
{
    public class DataCenter: Data
    {
        public DataCenter(string server, string user, string password, string dbname) : base(server, user, password, dbname) { }

        public DataCenter() { }

        public void LoadData()
        {
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from dcs";
                    ToJson(cmd);
                }
            }
        }

        private void Remove()
        {
            var id = GetId();
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "delete from dcs where id = @id";
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                }
                Result("RESULT_OK");
            }
        }

        private void Add()
        {
            var name = param("name");
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "insert into dcs (name, address, power, notes) values (@name, @address, @power, @notes); select @@identity;";
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@address", param("address"));
                    cmd.Parameters.AddWithValue("@power", param("power"));
                    cmd.Parameters.AddWithValue("@notes", param("notes"));
                    writer("{\"result\": \"RESULT_OK\", \"id\": \"" + cmd.ExecuteScalar().ToString() + "\"}");
                }
            }
        }

        private void Edit()
        {
            var id = GetId();
            var name = GetName();
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "update dcs set name = @name, address = @address, power = @power, notes = @notes where id = @id";
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@address", param("address"));
                    cmd.Parameters.AddWithValue("@power", param("power"));
                    cmd.Parameters.AddWithValue("@notes", param("notes"));
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                }
                Result("RESULT_OK");
            }
        }

        private void Resize()
        {
            var id = GetId();
            var width = GetWidth();
            var height = GetHeight();
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "update dcs set width = @width, height = @height where id = @id";
                    cmd.Parameters.AddWithValue("@width", width);
                    cmd.Parameters.AddWithValue("@height", height);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    Result("RESULT_OK");
                }
            }
        }

        private void MoveEntrance()
        {
            var id = GetId();
            var left = GetLeft();
            var top = GetTop();
            using (var conn = DbConnect())
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "update dcs set entrance_left = @left, entrance_top = @top where id = @id";
                    cmd.Parameters.AddWithValue("@left", left);
                    cmd.Parameters.AddWithValue("@top", top);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    ResultOK();
                }
            }
        }

        private int GetId()
        {
            return GetInt("id");
        }

        private int GetWidth()
        {
            return GetInt("width");
        }

        private int GetHeight()
        {
            return GetInt("height");
        }

        private int GetTop()
        {
            return GetInt("top");
        }

        private int GetLeft()
        {
            return GetInt("left");
        }

        private string GetName()
        {
            var name = param("name");
            if (string.IsNullOrEmpty(name))
            {
                Result("ERROR_EMPTY_NAME");
                throw new ArgumentException("Empty name");
            }
            return name;
        }

        public override void Process()
        {
            try
            {
                var mode = param("mode");
                switch (mode)
                {
                    case "edit": Edit(); break;
                    case "remove": Remove(); break;
                    case "add": Add(); break;
                    case "resize": Resize(); break;
                    case "move_entrance": MoveEntrance(); break;
                    default: LoadData(); break;
                }

            }
            catch (ArgumentException ex) { }
            catch (Exception ex)
            {
                writer(ex.ToString());
            }
        }

        public override void Headers(SetHeaderDelegate callback)
        {
            callback("Content-type", "application/json");
        }
    }
}